<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1. 咒语和药水的成功对数
        // 二分查找

        const searchRanage = function (nums, target) {
            // 开区间，left初始化为-1，right初始化为nums.length
            let left = -1;
            let right = nums.length
            let mid;
            // 开区间，这里写left + 1 < right
            while (left + 1 < right) {
                mid = left + ((right - left) >> 1)
                if (nums[mid] >= target) {
                    // 是mid，而不是mid-1
                    right = mid
                } else {
                    // 是mid，而不是mid+1
                    left = mid
                }
            }
            // 注意开区间返回的是right
            return right
        }
        var successfulPairs = function(spells, potions, success) {
            // 需要对药水进行排序
            potions.sort((a, b) => a - b)
            let ans = new Array(spells.length).fill(0)
            for (let i = 0; i < spells.length; i++) {
                // 假设咒语是5，success是7，Math.ceil(7 / 5) 是2, 5必须和2乘才能大于7
                let num = Math.ceil(success / spells[i])
                let target = searchRanage(potions, num)
                // 如果找不到能够相乘大于success的值，那么target会大于比potions
                if (target < potions.length) {
                    ans[i] = potions.length - target
                }
            }
            return ans
        };
        // console.log(successfulPairs([5,1,3], [1,2,3,4,5], 7), 'successfulPairs');
        console.log(successfulPairs([3,1,2], [8,5,8], 16), 'successfulPairs');
    </script>
</body>

</html>